package com.lxs.legou.order.controller;

import com.lxs.legou.core.controller.BaseController;
import com.lxs.legou.order.config.TokenDecode;
import com.lxs.legou.order.po.Order;
import com.lxs.legou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Auther: Mcb
 * @Date: 2022/1/23 18:00
 * @Description:
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController<OrderService,Order> {


    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 购物车下订单
     * @param order
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    public ResponseEntity add(@RequestBody Order order) throws IOException {
        order.setUsername(tokenDecode.getUserInfo().get("user_name"));
        service.add(order);
        return ResponseEntity.ok("下单成功");
    }

    /**
     * 订单支付成功，改变订单状态
     * @param orderId
     * @return
     * @throws IOException
     */
    @PostMapping("/pay")
    public ResponseEntity pay(@RequestParam("orderId") String orderId) {
        service.pay(orderId);
        return ResponseEntity.ok("支付成功");
    }
}
